﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MSBTC.Model;

namespace MSBTC.ListWinForms
{
    public partial class UserListForm : Form
    {
        private string Datamanip;
        private int rowselect;
        MDIParent mdipar = new MDIParent();
        PMSContext dbcontext = new PMSContext();
        Customer dbcus = new Customer();

        public UserListForm()
        {
            InitializeComponent();
        }

        private void UserListForm_Load(object sender, EventArgs e)
        {
            RefreshDB();
        }
        
        #region functions
        private void RefreshDB()
        {
            var use = (from u in dbcontext.Users
                       select u).ToList();

            LVUserListInfo.Items.Clear();
            if (use != null)
            {

                foreach (var sqlUser in use)
                {
                    ListViewItem lvitems = LVUserListInfo.Items.Add(sqlUser.Userid_PK.ToString());
                    lvitems.SubItems.Add(sqlUser.Username.ToString());
                    lvitems.SubItems.Add(sqlUser.Password.ToString());
                    lvitems.SubItems.Add(sqlUser.Name.ToString());
                    lvitems.SubItems.Add(sqlUser.Role_Name.ToString());
                    lvitems.SubItems.Add(sqlUser.Status.ToString());
                    

                }
            }
            this.Refresh();
        }



        private void DisableFields()
        {
            //txtCname.Enabled = false;
            //txtAddr.Enabled = false;
            //txtCity.Enabled = false;
            //txtDesc.Enabled = false;
            //txtEadd.Enabled = false;
            //txtCnum.Enabled = false;

        }

        private void EnableFields()
        {
            //txtCname.Enabled = true;
            //txtAddr.Enabled = true;
            //txtCity.Enabled = true;
            //txtDesc.Enabled = true;
            //txtEadd.Enabled = true;
            //txtCnum.Enabled = true;

        }

        private void RefreshFields()
        {
            DisableFields();
            RefreshDB();

            //btnCNew.Enabled = true;
            //btnCDel.Enabled = false;
            //btnCEdit.Enabled = false;
            //btnClear.Enabled = true;
            //btnCSave.Enabled = false;
        }
        #endregion

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            var use = (from u in dbcontext.Users
                       where u.Username.Contains(txtSearch.Text)
                       select u).ToList();

            LVUserListInfo.Items.Clear();
            if (use != null)
            {

                foreach (var sqlUse in use)
                {
                    ListViewItem lvitems = LVUserListInfo.Items.Add(sqlUse.Userid_PK.ToString());
                    lvitems.SubItems.Add(sqlUse.Username.ToString());
                    lvitems.SubItems.Add(sqlUse.Password.ToString());
                    lvitems.SubItems.Add(sqlUse.Name.ToString());
                    lvitems.SubItems.Add(sqlUse.Role_Name.ToString());
                    lvitems.SubItems.Add(sqlUse.Status.ToString());
                   

                }

                if (txtSearch.Text == "")
                {
                    RefreshDB();
                }
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
          
        }

       

    
    }
}
